package com.sys.platform.service.impl;

import cn.hutool.core.date.DateTime;
import com.sys.platform.common.utils.StringUtils;
import com.sys.platform.entity.DossierDevicePosition;
import com.sys.platform.mapper.DossierDevicePositionMapper;
import com.sys.platform.service.IDossierDevicePositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 档案馆设备位置关联Service业务层处理
 *
 * @author yuanfuqiang
 * @date 2024-01-18
 */
@Service
public class DossierDevicePositionServiceImpl implements IDossierDevicePositionService {

    @Autowired
    private DossierDevicePositionMapper dossierDevicePositionMapper;

    /**
     * 新增档案馆设备位置关联
     *
     * @param dossierDevicePosition 档案馆设备位置关联
     * @return 结果
     */
    @Override
    public int insertDossierDevicePosition(DossierDevicePosition dossierDevicePosition) {
        DossierDevicePosition position = new DossierDevicePosition();
        position.setDeviceId(dossierDevicePosition.getDeviceId());
        List<DossierDevicePosition> devicePositionList = dossierDevicePositionMapper.select(position);
        //不存在新增
        if (StringUtils.isEmpty(devicePositionList)) {
            dossierDevicePosition.setCreateTime(DateTime.now());
            return dossierDevicePositionMapper.insertSelective(dossierDevicePosition);
        }
        position = devicePositionList.get(0);
        dossierDevicePosition.setId(position.getId());
        dossierDevicePosition.setUpdateTime(DateTime.now());
        return dossierDevicePositionMapper.updateByPrimaryKeySelective(dossierDevicePosition);
    }

    /**
     * 修改档案馆设备位置关联
     *
     * @param dossierDevicePosition 档案馆设备位置关联
     * @return 结果
     */
    @Override
    public int updateDossierDevicePosition(DossierDevicePosition dossierDevicePosition) {
        dossierDevicePosition.setUpdateTime(DateTime.now());
        return dossierDevicePositionMapper.updateByPrimaryKeySelective(dossierDevicePosition);
    }

    /**
     * 批量删除档案馆设备位置关联
     *
     * @param ids 需要删除的档案馆设备位置关联主键
     * @return 结果
     */
    @Override
    public int deleteDossierDevicePositionByIds(Long[] ids) {
        AtomicInteger i = new AtomicInteger();
        Arrays.stream(ids).forEach(id -> {
            DossierDevicePosition dossierDevicePosition = new DossierDevicePosition();
            dossierDevicePosition.setDeviceId(id.toString());
            dossierDevicePositionMapper.delete(dossierDevicePosition);
            i.set(1);
        });
        return i.get();
    }

}
